﻿using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using JinndevEditor;
using System.Collections.Generic;

namespace Jinndev.UIEditor {

    public static class UIEditorMenu {

        [MenuItem(CommonEditorConst.EditorMenu + "/替换颜色")]
        public static void OpenReplaceColorEditor() {
            ReplaceColorEditor.Open();
        }

        [MenuItem(CommonEditorConst.AssetsMenu + "/创建UI代码+挂载到预设")]
        public static void CreateUIScript() {
            GameObject obj = Selection.activeGameObject;

            // 得到预设路径
            string path = AssetDatabase.GetAssetPath(obj);
            string dir = Path.GetDirectoryName(path);
            dir = CommonUtil.SubStringFrom(dir, "Prefab");

            // 得到代码文件名
            string name = obj.name;
            string file = Path.Combine(Application.dataPath, "Scripts" + dir, name + ".cs");
            string scriptAssetPath = "Assets" + file.Substring(Application.dataPath.Length);

            // 如果代码已挂载到预设上，则打开IDE
            System.Type type = System.Reflection.Assembly.Load("Assembly-CSharp").GetType("Jinndev." + name);
            if (type != null && obj.GetComponent(type) != null) {
                AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath<MonoScript>(scriptAssetPath));
                return;
            }

            // 如果代码文件已存在，则挂载到预设上，并打开IDE
            if (File.Exists(file)) {
                if (type != null) {
                    obj.AddComponent(type);
                    AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath<MonoScript>(scriptAssetPath));
                }
                else {
                    Selection.activeObject = AssetDatabase.LoadAssetAtPath<MonoScript>(scriptAssetPath);
                }
                return;
            }

            //AssemblyReloadEvents.afterAssemblyReload += ()=> {
            //    if(AddComponent(obj, "Jinndev." + name)) {
            //        AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath<MonoScript>(scriptAssetPath));
            //    }
            //};

            // 创建代码文件
            string prefabPath = CommonUtil.SubStringFromTo(path.Replace("\\", "/"), "Resources/", ".prefab");

            StringBuilder builder = new StringBuilder();
            builder.AppendLine("using System.Collections.Generic;");
            builder.AppendLine("using System.Collections;");
            builder.AppendLine("using UnityEngine;");
            builder.AppendLine("using UnityEngine.UI;");
            builder.AppendLine("using Jinndev.UI;");
            builder.AppendLine();
            builder.AppendLine("namespace Jinndev {");
            builder.AppendLine();
            builder.AppendLine($"    public class {name} : BaseUIBehaviour {{");
            builder.AppendLine();
            builder.AppendLine("        #region static");
            builder.AppendLine($"        public static readonly string PATH = \"{prefabPath}\";");
            builder.AppendLine();
            builder.AppendLine($"        public static {name} Create() {{");
            builder.AppendLine($"            return UIManager.Instance.Add<{name}>(PATH, UIParam.FullScreen);");
            builder.AppendLine("        }");
            builder.AppendLine("        #endregion");
            builder.AppendLine();
            builder.AppendLine("        private void Awake() {");
            builder.AppendLine();
            builder.AppendLine("        }");
            builder.AppendLine();
            builder.AppendLine("    }");
            builder.AppendLine();
            builder.AppendLine("}");

            dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir)) {
                Directory.CreateDirectory(dir);
            }
            File.WriteAllText(file, builder.ToString(), Encoding.UTF8);
            AssetDatabase.Refresh();
        }

        [MenuItem(CommonEditorConst.AssetsMenu + "/创建UI代码+挂载到预设", true)]
        public static bool _CreateUIScript() {
            GameObject obj = Selection.activeGameObject;
            return obj != null && obj.GetComponent<RectTransform>() != null;
            //return obj != null && obj.GetComponent<RectTransform>() != null && obj.name != "Canvas (Environment)" && (obj.transform.parent == null || obj.transform.parent.name == "Canvas (Environment)");
        }


    }

}
